package com.vrv.controller.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vrv.entity.Form;
import com.vrv.entity.FormLog;
import com.vrv.utils.HttpClientUtil;
import com.vrv.utils.JsonUtil;
import com.vrv.utils.XMLRead;

@Controller
@RequestMapping("/cluster")
public class ClusterSetController {

    /**
     * Description: <br>
     * 集群列表
     * 
     * @return <br>
     */
    @RequestMapping("/index")
    public String list() {
        return "/server/cluster_list";
    }

    /**
     * Description: <br>
     * 集群信息
     * 
     * @return <br>
     */
    @RequestMapping("/info")
    public String getServerInfo(HttpServletRequest request, String servId, String serviceName) {
        request.setAttribute("serviceName", "服务 " + serviceName);
        return "/server/server_info";
    }

    /**
     * Description: <br>
     * 集群参数配置
     * 
     * @return <br>
     */
    @RequestMapping("/parameter")
    public String getParameter(HttpServletRequest request, String servId, String serviceName,
            String ip) {
        request.setAttribute("serviceName", serviceName);
        request.setAttribute("ip", XMLRead.AIM);
        return "/server/parameter_info";
    }

    /**
     * Description: <br>
     * 集群模板配置
     * 
     * @return <br>
     */
    @RequestMapping("/template")
    public String getTemplate(HttpServletRequest request, String servId, String serviceName,
            String ip) {
        request.setAttribute("serviceName", serviceName);
        request.setAttribute("ip", XMLRead.AIM);
        return "/server/template_info";
    }

    @RequestMapping(value = "getServicesList", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getServicesList(HttpServletResponse response) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> list = new ArrayList<String>();
        list.add("hadoop");
        params.put("target", XMLRead.AIM);
        params.put("option", "getallservices");
        params.put("services", "all");
        // params.put("object", "parameters");

        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "getNodeStatus", produces = "application/json;charset=UTF-8")
    public @ResponseBody String getNodeStatus(HttpServletResponse response, String serviceName,
            String ip) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> list = new ArrayList<String>();
        list.add(serviceName);
        params.put("target", ip);
        params.put("option", "get");
        params.put("services", list);
        params.put("object", "status");

        String json = JsonUtil.MapToJson(params);

        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "startNodeByIpAndServiceName",
            produces = "application/json;charset=UTF-8")
    public @ResponseBody String startNodeByIpAndServiceName(HttpServletResponse response,
            String serviceName, String ip) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> serviceList = new ArrayList<String>();
        serviceList.add(serviceName);
        params.put("target", ip);
        params.put("option", "start");
        params.put("services", serviceList);
        params.put("object", "status");
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "stopNodeByIpAndServiceName",
            produces = "application/json;charset=UTF-8")
    public @ResponseBody String stopNodeByIpAndServiceName(HttpServletResponse response,
            String serviceName, String ip) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> serviceList = new ArrayList<String>();
        serviceList.add(serviceName);
        params.put("target", ip);
        params.put("option", "stop");
        params.put("services", serviceList);
        params.put("object", "status");
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "updateState", produces = "application/json;charset=UTF-8")
    public @ResponseBody String updateState(HttpServletResponse response, String serviceName,
            String ip, Integer state) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> serviceList = new ArrayList<String>();
        serviceList.add(serviceName);
        params.put("target", ip);
        params.put("option", "set");
        params.put("services", serviceList);
        params.put("object", "state");
        Map<String, Object> stateMap = new HashMap<String, Object>();
        stateMap.put("state", state);
        Map<String, Object> serviceMap = new HashMap<String, Object>();
        serviceMap.put(serviceName, stateMap);
        params.put("set", serviceMap);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "showCells", produces = "application/json;charset=UTF-8")
    public @ResponseBody String showCells(HttpServletResponse response, String serviceName,
            String ip) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> serviceList = new ArrayList<String>();
        serviceList.add(serviceName);
        params.put("target", ip);
        params.put("option", "get");
        params.put("services", serviceList);
        params.put("object", "parameters");
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "updateCells", produces = "application/json;charset=UTF-8")
    public @ResponseBody String updateCells(HttpServletResponse response, Form keyArray,
            Form valueArray, String serviceName, String ip) {
        List<String> keyList = keyArray.getKeyArray();
        List<String> valueList = keyArray.getValueArray();
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> list = new ArrayList<String>();
        list.add(serviceName);
        params.put("target", ip);
        params.put("services", list);
        params.put("option", "set");
        Map<String, Object> cellParams = new HashMap<String, Object>();
        for (int i = 0; i < keyList.size(); i++) {
            cellParams.put(keyList.get(i), valueList.get(i));
        }
        // List<Map<String, Object>> setList = new ArrayList<Map<String, Object>>();
        Map<String, Object> serviceMap = new HashMap<String, Object>();
        Map<String, Object> parametersMap = new HashMap<String, Object>();
        parametersMap.put("parameters", cellParams);
        serviceMap.put(serviceName, parametersMap);
        // setList.add(serviceMap);
        params.put("set", serviceMap);
        params.put("object", "parameters");
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "showModel", produces = "application/json;charset=UTF-8")
    public @ResponseBody String showModel(HttpServletResponse response, String serviceName,
            String ip) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> serviceList = new ArrayList<String>();
        serviceList.add(serviceName);
        params.put("target", ip);
        params.put("option", "get");
        params.put("services", serviceList);
        params.put("object", "template");
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "updateModel", produces = "application/json;charset=UTF-8")
    public @ResponseBody String showModel(HttpServletResponse response, String serviceName,
            String ip, String modelContent) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> serviceList = new ArrayList<String>();
        serviceList.add(serviceName);
        params.put("target", ip);
        params.put("option", "set");
        params.put("services", serviceList);
        params.put("object", "template");
        Map<String, Object> mapParam = new HashMap<String, Object>();
        mapParam.put("template", modelContent);
        Map<String, Object> serviceParam = new HashMap<String, Object>();
        serviceParam.put(serviceName, mapParam);
        // Map<String,Object> setParam = new HashMap<String,Object>();
        // setParam.put("set", serviceParam);
        params.put("set", serviceParam);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "showProgramOutput", produces = "application/json;charset=UTF-8")
    public @ResponseBody String showProgramOutput(HttpServletResponse response, FormLog form) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> serviceList = new ArrayList<String>();
        serviceList.add(form.getServiceName());
        params.put("target", form.getIp());
        params.put("option", "log");
        params.put("services", serviceList);
        params.put("object", "log");

        Map<String, Object> mapObject = new HashMap<String, Object>();
        mapObject.put("type", form.getType());
        mapObject.put("file", form.getFile());
        mapObject.put("startline", form.getStartline());
        mapObject.put("linenum", form.getLinenum());
        mapObject.put("totalnum", form.getTotalnum());
        Map<String, Object> mapObject2 = new HashMap<String, Object>();
        mapObject2.put(form.getServiceName(), mapObject);
        params.put("log", mapObject2);
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

    @RequestMapping(value = "showLogList", produces = "application/json;charset=UTF-8")
    public @ResponseBody String showLogList(HttpServletResponse response, String serviceName,
            String ip) {
        Map<String, Object> params = new HashMap<String, Object>();
        List<String> serviceList = new ArrayList<String>();
        serviceList.add(serviceName);
        params.put("target", ip);
        params.put("option", "loglist");
        params.put("services", serviceList);
        params.put("object", "loglist");
        String json = JsonUtil.MapToJson(params);
        String str = HttpClientUtil.getInstance().post(
                "http://" + XMLRead.AIM + ":56790/?localservice", json);
        return str;
    }

}
